package org.yzl.designPatterns.iterator.impl;

import org.yzl.designPatterns.iterator.ICollection;
import org.yzl.designPatterns.iterator.IIterator;

import java.util.Collection;

public class IteratorImpl implements IIterator {

    private ICollection collection;
    private int pos = -1;

    public IteratorImpl(ICollection collection){
        this.collection = collection;
    }


    @Override
    public Object previous() {
        if(pos > 0){
            pos-- ;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if(pos < (collection.size()-1)){
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        if(pos < (collection.size()-1)){
            return true;
        }
        return false;
    }

    @Override
    public Object first() {
        pos = 0;
        return collection.get(pos);
    }
}
